﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace EventScavenger
{
    public partial class ViewEditor : Form
    {
        private EventScavengerDAL sqlEventSource;

        #region Properties
        private EventLogView selectedView = new EventLogView();
        public EventLogView SelectedView
        {
            get { return selectedView; }
            set { selectedView = value; }
        } 
        #endregion

        public DialogResult Show(EventScavengerDAL sqlEventSource)
        {
            this.sqlEventSource = sqlEventSource;
            return ShowDialog();
        }

        public ViewEditor()
        {
            InitializeComponent();
        }

        #region Form events
        private void ViewEditor_Load(object sender, EventArgs e)
        {
            LoadViewDetails();
        }
        #endregion

        #region Button events
        private void cmdPathBrowse_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = txtViewPath.Text;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtViewPath.Text = saveFileDialog1.FileName;
                ValidateOK();
            }
        }
        private void cmdLogsFilter_Click(object sender, EventArgs e)
        {
            MachineLogFilter machineLogFilter = new MachineLogFilter();
            machineLogFilter.AllEnabled = selectedView.ShowAllLogs;
            machineLogFilter.SelectedMachineLogs = selectedView.MachineLogs;
            if (machineLogFilter.Show(sqlEventSource) == DialogResult.OK)
            {
                selectedView.ShowAllLogs = machineLogFilter.AllEnabled;

                if (selectedView.ShowAllLogs)
                    selectedView.MachineLogs = new List<MachineLogEntry>();
                else
                    selectedView.MachineLogs = machineLogFilter.SelectedMachineLogs;
                LoadMachineLogDetails();
            }
            ValidateOK();
        }
        private void cmdSourceFilter_Click(object sender, EventArgs e)
        {
            EventSourceFilter eventSourceFilter = new EventSourceFilter();
            eventSourceFilter.AllEnabled = selectedView.ShowAllSources;
            eventSourceFilter.SelectedSources = selectedView.Sources;
            if (eventSourceFilter.Show(sqlEventSource) == DialogResult.OK)
            {
                selectedView.ShowAllSources = eventSourceFilter.AllEnabled;
                if (selectedView.ShowAllSources)
                    selectedView.Sources = new List<string>();
                else
                    selectedView.Sources = eventSourceFilter.SelectedSources;
                LoadSourcesDetails();
            }
            ValidateOK();
        }
        private void cmdEventIDFilter_Click(object sender, EventArgs e)
        {
            EventIDFilter eventIDFilter = new EventIDFilter();
            eventIDFilter.AllEnabled = selectedView.ShowAllEventIDs;
            eventIDFilter.SelectedIds = selectedView.EventIds;
            if (eventIDFilter.Show(sqlEventSource) == DialogResult.OK)
            {
                selectedView.ShowAllEventIDs = eventIDFilter.AllEnabled;
                if (selectedView.ShowAllEventIDs)
                    selectedView.EventIds = new List<int>();
                else
                    selectedView.EventIds = eventIDFilter.SelectedIds;
                LoadEventIDDetails();
            }
            ValidateOK();
        }
        private void cmdOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateOK())
                {
                    selectedView.Name = txtName.Text;
                    selectedView.ViewPath = txtViewPath.Text;
                    selectedView.EventLogEntryType = cboType.SelectedIndex;
                    selectedView.FilterText = txtTextFilter.Text;

                    string serialized = Serializer.SerializeXML(selectedView);
                    System.IO.File.WriteAllText(txtViewPath.Text, serialized);
                    DialogResult = DialogResult.OK;
                    Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error saving view\r\n" + ex.Message, "Saving", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Combo box events
        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ValidateOK();
        } 
        #endregion

        #region Text box events
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            ValidateOK();
        } 
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (txtViewPath.Text.Length == 0)
            {
                txtViewPath.Text = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), txtName.Text + ".elvw");
                ValidateOK();
            }
        }
        private void txtViewPath_Leave(object sender, EventArgs e)
        {
            ValidateOK();
        }
        private void txtTextFilter_TextChanged(object sender, EventArgs e)
        {
            ValidateOK();
        }
        #endregion

        #region Private methods
        private bool ValidateOK()
        {
            bool okEnabled = true;
            if (txtName.Text.Length == 0)
            {
                okEnabled = false;
            }
            if (txtViewPath.Text.Length == 0)
            {
                okEnabled = false;
            }
            else if (!Directory.Exists(Path.GetDirectoryName(txtViewPath.Text)))
            {
                okEnabled = false;
            }
            if (cboType.SelectedIndex == -1)
            {
                okEnabled = false;
            }

            cmdOK.Enabled = okEnabled;
            return okEnabled;
        }
        private void LoadViewDetails()
        {
            if (selectedView != null)
            {
                txtName.Text = selectedView.Name;
                txtViewPath.Text = selectedView.ViewPath;
                cboType.SelectedIndex = selectedView.EventLogEntryType;
                txtTextFilter.Text = selectedView.FilterText;

                LoadMachineLogDetails();
                LoadEventIDDetails();
                LoadSourcesDetails();
                ValidateOK();
            }
        }
        private void LoadMachineLogDetails()
        {
            StringBuilder sb;
            txtLogs.Text = "";
            sb = new StringBuilder();
            if (!selectedView.ShowAllLogs)
            {
                foreach (MachineLogEntry mle in selectedView.MachineLogs)
                {
                    sb.Append(mle.Machine + "\\" + mle.LogName + ",");
                }
                txtLogs.Text = sb.ToString().Trim(',');
            }
        }
        private void LoadEventIDDetails()
        {
            StringBuilder sb;
            txtEventIDs.Text = "";
            sb = new StringBuilder();
            if (!selectedView.ShowAllEventIDs)
            {
                foreach (int eventId in selectedView.EventIds)
                {
                    sb.Append(eventId.ToString() + ",");
                }
                txtEventIDs.Text = sb.ToString().Trim(',');
            }
        }
        private void LoadSourcesDetails()
        {
            StringBuilder sb;
            txtSources.Text = "";
            sb = new StringBuilder();
            if (!selectedView.ShowAllSources)
            {
                foreach (string source in selectedView.Sources)
                {
                    sb.Append(source + ",");
                }
                txtSources.Text = sb.ToString().Trim(',');
            }
        } 
        #endregion


    }
}
